package com.cuiweiyou.nighttheme;

import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuiweiyou.nighttheme.ThemeLogic.ThemeChangeListener;

/**
 * 需动态切换主题的Aty都实现监听器
 */
public class MainActivity extends Activity implements ThemeChangeListener {

	private TextView tv;
	private ImageView img;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		/** 1。!!! 在setContentView之前setTheme ！！！ */
		setTheme(R.style.day);
		
		/** 2。设置布局 */
		setContentView(R.layout.activity_main);
		
		tv = (TextView) findViewById(R.id.tv);
		img = (ImageView) findViewById(R.id.img);

		/** 3。备案 */
		ThemeLogic.getInstance().addListener(this);
		
		startActivity(new Intent(MainActivity.this, ThemeActivity.class));
	}

	/**
	 * 4。当执行切换时，回调此方法，改变控件可视状态
	 */
	@Override
	public void onThemeChanged() {
		switch (ThemeLogic.themeType) {
			case 1:
				setTheme(R.style.day);
			break;
			case 2:
				setTheme(R.style.night);
			break;
		}
		
		// 加载自定义属性
		TypedArray typedArray = obtainStyledAttributes(R.styleable.myStyle);
		tv.setTextColor(typedArray.getColor(R.styleable.myStyle_mycolor, Color.BLACK));
		img.setImageDrawable(typedArray.getDrawable(R.styleable.myStyle_mypic));
		typedArray.recycle();
	}
}
